package demo_0714;

public class T15_0330 {
    //交换
    private static void swap(int[] array,int i,int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
    // 冒泡排序
    public static void bubbleSort(int[] array){
        // write code  here
        int len = array.length;
        for (int i = 0; i < len - 1; i++) {
            boolean flg = true;
            for (int j = 0; j < len - 1 - i; j++) {
                if (array[j] > array[j + 1]) {
                    swap(array,j,j+1);
                    flg = false;
                }
            }
            if (flg) return;
        }
    }

    // 快速
    public static void quickSort(int[] array){
        // write code  here
        qs(array,0,array.length - 1);
    }
    private static void qs (int[] arr, int left, int right) {
        if (left >= right) return;
        if (right - left + 1 < 10) {
            insertSortRange(arr,left,right);
            return;
        }
        int index = getMid(arr,left,right);
        swap(arr,left,index);
        int pivot = partion(arr,left,right);
        qs(arr,left,pivot - 1);
        qs(arr,pivot + 1,right);
    }
    private static int partion(int[] arr, int left, int right) {
        int pivot = arr[left];
        while (left < right) {
            while (left < right && arr[right] >= pivot) {
                right--;
            }
            arr[left] = arr[right];
            while (left < right && arr[left] <= pivot) {
                left++;
            }
            arr[right] = arr[left];
        }
        arr[left] = pivot;
        return left;
    }

    private static int getMid(int[] arr,int left,int right) {
        int mid = (left + right) / 2;
        if (arr[left] < arr[right]) {
            if (arr[mid] > arr[right]) {
                return right;
            } else if (arr[mid] < arr[left]) {
                return left;
            } else {
                return mid;
            }
        } else {
            if (arr[mid] < arr[right]) {
                return right;
            } else if (arr[mid] > arr[left]) {
                return left;
            } else {
                return mid;
            }
        }
    }


    // 插入排序
    public static void insertSortRange(int[] array, int left, int right){
        // write code  here
        for (int i = left + 1; i <= right; i++) {
            int j, tmp = array[i];
            for (j = i - 1; j >= left; j--) {
                if (array[j] > tmp) {
                    array[j + 1] =  array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    public static void main(String[] args) {
        int[] arr = {1,5,10,2,3,4,6,7,8,9};
        quickSort(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
        int[] arr1 = {10,9,8,7,6,5,4,3,2,1};
        bubbleSort(arr1);
        for (int i = 0; i < arr1.length; i++) {
            System.out.print(arr1[i] + " ");
        }
        System.out.println();
    }
}
